/**
 * 
 */
package com.ccqlk.attendance.controller.manage.company;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccqlk.attendance.common.page.PageRequest;
import com.ccqlk.attendance.controller.manage.AbstractManageController;
import com.ccqlk.attendance.model.Department;
import com.ccqlk.attendance.model.Staff;
import com.ccqlk.attendance.repository.service.DepartmentService;
import com.ccqlk.attendance.repository.service.StaffService;
import com.github.pagehelper.PageInfo;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: StaffManageController<br>
 * 文件说明: TODO<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月28日 下午11:10:38<br>
 */
@Controller
@RequestMapping("manage/company/staff")
public class StaffManageController extends AbstractManageController<Staff, Integer> {

	private StaffService staffService;
	private @Resource DepartmentService departmentService;
	
	@Autowired
	public StaffManageController(StaffService staffService) {
		super(staffService, "manage/company/staff/", "staff");
		this.staffService = staffService;
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#getData(com.ccqlk.attendance.common.page.PageRequest, javax.servlet.http.HttpServletRequest, org.springframework.ui.Model, boolean)
	 */
	@Override
	public PageInfo<Staff> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		return staffService.selectAll(pageRequest);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#listHandle(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void listHandle(HttpServletRequest request) {
		List<Department> departments = departmentService.selectAll();
		request.setAttribute("departmentList", departments);
	}
	
	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#addJson(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.ui.Model, org.springframework.validation.BindingResult)
	 */
	@RequestMapping("addJson")
	@ResponseBody
	@Override
	public Model addJson(HttpServletRequest request, Staff entity, Model model, BindingResult result) {
		model = super.addJson(request, entity, model, result);
		if (entity!=null) {
			String code = StringUtils.leftPad(entity.getId()+"", 8, "0");
			entity.setCode(code);
			staffService.edit(entity);
		}
		return model;
	}
}
